import {apiAddress,MUDOMAIN,DOMAIN} from '~/util/config.js'
export default function ({
	redirect,
	req,
	route,
	store
}) {
	let current_path = process.server ? req.url.split('?')[0] : route.path;
	//判断用户的浏览器是否为ie浏览器或Edge浏览器
	function isIE() {
		const userAgent = process.server ? req.headers['user-agent'] : navigator.userAgent;
		let IsIE = userAgent.indexOf("MSIE") > -1 || userAgent.indexOf('Trident') > -1;
		let IsEDGE = userAgent.indexOf("Edge") > -1;
		if (IsIE || IsEDGE) {
			return true;
		} else {
			return false;
		}
	}
	if (isIE()) {
		redirect('/ieTips')
	} else {
		if (current_path.indexOf('ieTips') > -1) {
			redirect('/')
		}
	}

	// 	//判断浏览器是pc端还是移动端
	function IsPC() {
		var userAgentInfo = process.server ? req.headers["user-agent"] : navigator.userAgent;
		var Agents = ["Android", "iPhone",
			"SymbianOS", "Windows Phone",
			"iPad", "iPod"
		];
		var flag = true;
		for (var v = 0; v < Agents.length; v++) {
			if (userAgentInfo.indexOf(Agents[v]) > 0) {
				flag = false;
				break;
			}
		}
		return flag;
	}
		const PC_BASE = DOMAIN
	const MOBILE_BASE = MUDOMAIN
	let path2 = route.fullPath
    if (process.env.NODE_ENV == 'production') {
		console.log(path2)
		let path = process.server ? req.url.split('?')[0] : route.path;
		if(!IsPC){
			let index=path2.lastIndexOf('=')
			let path3=path2.replace(RegExp("%2F",'g'),'/').substring(index+1,path2.length)
			redirect(MOBILE_BASE+`${path3}`)
		}else{
			if (path.substring(0, 5) == '/user' || (path == '/') || (path == '/register')) {
				if (path == '/user/apply') {
					if (!IsPC()) {
						return !(IsPC()) ? '' : redirects(MOBILE_BASE + '/user/apply');
					}
				} else {
					if (!IsPC()) {
						redirect(MOBILE_BASE + `${path2}`)
					}
				}
			} else if (path.indexOf('/order/login') > -1) {
				if (!IsPC()) {
					redirect(`${MOBILE_BASE}/order/login`)
				}
			} else if (path.indexOf('preRegister') > -1) {
				if (!IsPC()) {
					redirect(`${MOBILE_BASE}/preRegiest`)
				}
			} else if (path.substring(0, 6) == '/admin' || (path == '/admin') || path.substring(0, 12) == '/admin/login') {
				if (!IsPC()) {
					redirect(MOBILE_BASE)
				}
			}else if(path.substring(0,6)=='/agent' || (path=='/agent')){
				if(!IsPC()){
					redirect(MOBILE_BASE+ `${path2}`)
				}
			}
		}
		
	}
	// // 测试
	// else{
	// 	//移动端打开pc端网页强制跳转移动端
	// 	let path = process.server ? req.url.split('?')[0] : route.path;
	// 	if (path.substring(0, 5) == '/user' || (path == '/') || (path == '/register')) {
    //         if (path == '/user/apply') {
    //             if (!IsPC()) {
    //                 return !IsPC() ? '' : redirects(MOBILE_BASE + '/user/apply');
    //             }
    //         } else {
    //             if (!IsPC()) {
    //                 redirect(MOBILE_BASE)
    //             }
    //         }
    //     } else if (path.indexOf('/order/login') > -1) {
	// 		if (!IsPC()) {
	// 			redirect(`${MOBILE_BASE}/order/login`)
	// 		}
	// 	} else if (path.indexOf('preRegister') > -1) {
	// 		if (!IsPC()) {
	// 			redirect(`${MOBILE_BASE}/preRegiest`)
	// 		}
	// 	} else if (path.substring(0, 6) == '/admin' || (path == '/admin') || path.substring(0, 12) == '/admin/login') {
    //         if (!IsPC()) {
    //             redirect(MOBILE_BASE)
    //         }
    //     } else if(path.indexOf('/user/news/notice')>-1){
	// 		if(!IsPc()){
	// 			redirect(`${MOBILE_BASE}/user/news/notice`)
	// 		}
	// 	}
	// }
	

	// if (process.env.NODE_ENV == 'production') { // 如果是生产环境
	// 	let path = process.server ? req.url.split('?')[0] : route.path;
	// 	let host = process.env.PWD;
	// 	let pc_base = 'https://xhhg-ip.com';
	// 	let m_base = 'http://m.xhhg-ip.com';
	// 	if (host && host.indexOf('test') > -1) {
	// 		pc_base = 'http://test.xhhg-ip.com';
	// 		m_base = 'http://test.m.xhhg-ip.com';
	// 	}
	// 	if (path.substring(0, 5) == '/user' || (path == '/') || (path == '/register')) {
	// 		if (path == '/user/apply') {
	// 			return IsPC() ? '' : redirect(m_base + '/user/apply');
	// 		} else {
	// 			return IsPC() ? '' : redirect(m_base);
	// 		}
	// 	}
	// 	// else if (path.indexOf('order/login') > -1) {
	// 	// 	if (!IsPC()) {
	// 	// 		redirect(m_base + '/order/login')
	// 	// 	} else {
	// 	// 		return false
	// 	// 		// test.m.xhhg-ip.com
	// 	// 		redirect(pc_base + '/order/login')
	// 	// 	}
	// 	// }
	// 	else if (path.substring(0, 6) == '/admin' || (path == '/admin') || path.substring(0, 12) == '/admin/login') {
	// 		return IsPC() ? '' : redirect(m_base + '/admin/login');
	// 	}
	// }
}
